import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const StatusData = [{ name:'已申请', value:'1' },{ name:'申请中', value:'0' },{ name:'已过期', value:'-1' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '物料名称',		name: 'material', 		type:'select', loading:true },
			{ label: '检测组',		name: 'pproject', 		type:'select', loading:true, idStr:'uuid' },
			{ label: '申请人',		name: 'apply_user' },
			{ label: '状态',		name: 'apply_status', 	type:'select', data:StatusData },
			{ label: '采购时间',		names: ['start_date','end_date'],	type:'date-range' },
		],
		// 提交表单列表
		formData : [
			{ label: '物料名称',		name: 'mate_id', 			half:true, 	required:true, 	type:'select' },
			{ label: '申请数量',		name: 'apply_qty', 			half:true, 	required:true },
			{ label: '申请人',		name: 'apply_user', 		half:true,	required:true },
			{ label: '申请时间',		name: 'apply_for_time', 	half:true, 	required:true, 	type:'date-time' },
			{ label: '检测组',		name: 'pgroup_id', 			half:true, 	required:true, 	type:'select', idStr:'uuid' },
			{ label: '审核人',		name: 'reviewer', 			half:true, 	required:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		this.fetch()
	}
	
	// 获取下拉数据
	getSelect = async () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 物料名称
			$fn.setRow(forms, 'material', await f.getCache({ api:'material' } ) )
			// 检测组
			$fn.setRow(forms, 'pproject', await f.getCache({ api:'ke_shi' } ) )
			this.setState({ forms })
		})
	}
	// 获取弹框下拉
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, [ 'mate_id' ], ()=>{
			$cache.then( async f => {
				// 物料名称
				$fn.setRow(formData, 'mate_id', await f.getCache({ api:'material' } ) )
				// 检测组
				$fn.setRow(formData, 'pgroup_id', await f.getCache({ api:'ke_shi' } ) )
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'mt-apply-form/index')
	
	// 表单
	cols = [
		{ title: '申请单', 		dataIndex: 'apply_number', 		width:150 },
		{ title: '申请时间', 	dataIndex: 'apply_for_time', 	...$fn.table.time },
		{ title: '物料名称', 	dataIndex: 'mate_name', 		width:150 },
		{ title: '物料编号', 	dataIndex: 'mate_number', 		width:150 },
		{ title: '申请数量', 	dataIndex: 'apply_qty', 		...$fn.table.num },
		{ title: '申请码', 		dataIndex: 'encrypt_code', 		width:150 },
		{ title: '审核人', 		dataIndex: 'reviewer', 			...$fn.table.name },
		{ title: '检测组', 		dataIndex: 'project_name', 		width:150 },
		{ title: '状态', 		dataIndex: 'apply_status', 		...$fn.table.status },
	]
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this, { click: this.getModalSelect }),				// 添加
			Controls.back.call(this),												// 返回
		]
	}
	
	render(){
		return (
			<>
				<Page title='申请单列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { type: -1 }) }
				</Page>
				{/* 添加 */}
				{ AddEditModal.call(this,{ labelWidth:'s7', width:800, api: 'mt-apply-form/add'}) }
			</>
		)
	}
}

export default Index